package com.fas.core;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import org.junit.Assert;

import com.fas.config.AppConfig;
import com.fas.core.constant.R;
import com.fas.core.util.sequence.Sequence;
import com.fas.hehe.system.model.LoginUser;
import com.fas.hehe.system.model.OperateLog;
import com.fas.hehe.system.model.OperateLogDetail;
import com.jfinal.log.Log;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.DbKit;
import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.Table;
import com.jfinal.plugin.activerecord.TableMapping;

/**
 * 
 * @ClassName: BaseService 
 * @Description: 业务处理基类
 * @Copyright: Copyright (c) 2016 FAS 
 * @anthor yixiaoping<thinker1013@163.com>
 * @since 2016年1月24日 下午11:02:47
 * @version V1.0
 */
public class BaseService extends Service {
	protected Log log = Log.getLog(getClass());
	

	//配置事务拦截器
	public void saveOperateLog(R.OperType operateType, Model<?> modelEntity,Model<?> oldModelEntity, LoginUser loginUser) {

		Assert.assertNotNull("modelEntity为空", modelEntity);
		Assert.assertNotNull("loginUser为空", loginUser);

		Table table = TableMapping.me().getTable(modelEntity.getClass());
		Assert.assertNotNull("获取Model对应Table为空", table);
		String tableName = table.getName();
		String tableSchema = AppConfig.me().getJdbcSchema();

		final String sql1 = "select column_name, column_comment from information_schema.columns where table_schema = ? and table_name = ?";
		List<Record> columnCommontRecords = Db.use(DbKit.MAIN_CONFIG_NAME).find(
				sql1, tableSchema, tableName);
		HashMap<String, String> columnCommonMap = new HashMap<String, String>();
		if (columnCommontRecords != null) {
			for (Record columnComment : columnCommontRecords) {
				columnCommonMap.put(columnComment.getStr("column_name"),
						columnComment.getStr("column_comment"));
			}
		}

		final String sql2 = "select table_name,table_comment from information_schema.tables where table_schema = ? and table_name = ?";
		Record tableCommontRecord = Db.use(DbKit.MAIN_CONFIG_NAME)
				.findFirst(sql2, tableSchema, tableName);
		String tableComment = tableCommontRecord.getStr("table_comment");

		String[] primaryKey = table.getPrimaryKey();
		Object pkValue = modelEntity.get(primaryKey[0]);
		OperateLog operlog = new OperateLog();
		Long logid = Sequence.getSequence();
		
		operlog.setLogId(logid);
		operlog.setAccount(loginUser.getPramaryPrincipal());
		operlog.setCompanyId(loginUser.getCompanyId());
		operlog.setLogType(operateType.getCode());
		operlog.setTableName(tableName);
		operlog.setTableComment(tableComment);
		operlog.setLogTime(new Date());
		operlog.setPrimaryKey(pkValue.toString());
		operlog.save();

		String[] attrNames = modelEntity._getAttrNames();
		ArrayList<OperateLogDetail> toSaveList = new ArrayList<OperateLogDetail>();
		for (String attrName : attrNames) {
			OperateLogDetail detail = new OperateLogDetail();
			detail.setId(Sequence.getSequence());
			detail.setLogId(logid);
			detail.setColumnName(attrName);
			detail.setColumnComment( columnCommonMap.get(attrName));
			detail.setOldValue(oldModelEntity != null ? oldModelEntity.get(attrName)+"" : "");
			detail.setNewValue(modelEntity.get(attrName)+"");
			
			
			toSaveList.add(detail);
		}
		
		Db.batchSave(toSaveList, R.Const.DB_BATCH_SIZE);
	}
	
	public String getLikeValue(Object value){
		return "%"+(String)value+"%";
	}
	
}
